<?php
namespace app\common\model;

use think\Db;
use think\Validate;

class ShopGoodsGevalModel extends BaseModel
{
    protected $name = 'shop_goods_geval'; //定义栏目表
    //开启自动写入时间戳
    protected $autoWriteTimestamp = false;
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = []; //插入
    protected $update = []; //修改

    protected $type = ['geval_image' => 'array'];

    /**
     * 添加订单评价
     * @param $orderId
     * @param $shippingScores
     * @param $serverScores
     * @param $gevalScores
     * @param $gevalContent
     * @param $gevalImage
     * @param int $isAnonymous
     */
    public function addGeval($orderId, $shippingScores, $serverScores, $gevalScores, $gevalContent, $gevalImage, $isAnonymous = 0)
    {
        $orderModel      = new ShopOrderModel();
        $orderGoodsModel = new ShopOrderGoodsModel();
        $goodsModel      = new ShopGoodsModel();
        $orderInfo       = $orderModel->where('order_id', $orderId)->find();
        $orderGoodsList  = $orderGoodsModel->where('order_id', $orderInfo['order_id'])->column('*', 'sku_id');
        $goodsIds        = [];
        //评价添加
        foreach ($orderGoodsList as $k => $v) {
            $data[$k]['site_id']         = $orderInfo['site_id'];
            $data[$k]['buyer_uid']       = $orderInfo['buyer_uid'];
            $data[$k]['buyer_username']  = $orderInfo['buyer_username'];
            $data[$k]['order_id']        = $orderInfo['order_id'];
            $data[$k]['order_sn']        = $orderInfo['order_sn'];
            $data[$k]['sku_id']          = $v['sku_id'];
            $data[$k]['goods_id']        = $v['goods_id'];
            $data[$k]['goods_name']      = $v['goods_name'];
            $data[$k]['sku_name']        = $v['sku_name'];
            $data[$k]['shipping_scores'] = $shippingScores;
            $data[$k]['server_scores']   = $serverScores;
            $data[$k]['geval_scores']    = $gevalScores;
            $data[$k]['geval_content']   = $gevalContent;
            $data[$k]['buyer_time']      = $orderInfo['create_time'];
            $data[$k]['geval_time']      = time();
            $data[$k]['is_anonymous']    = $isAnonymous;
            $data[$k]['geval_status']    = 0;
            $data[$k]['geval_image']     = $gevalImage;
            $goodsIds[]                  = $v['goods_id'];
        }
        $this->saveAll($data);
        $orderInfo['geval_status'] = 1;
        $orderModel->editData($orderInfo->toArray()); //修改订单评价状态
        $goodsModel->where('goods_id', 'in', $goodsIds)->setInc('goods_geval_number', 1); //商品评价数+1
        return true;
    }
}
